package com.pcbly.web.components.utils.template.pagemess;

import java.io.File;
import java.lang.reflect.Field;

import org.apache.commons.io.FileUtils;

public class ModelPageView extends ModelPage {

	private String viewCol = "";
	
	private String viewDateCol = "";
	
	private String viewDoubleCol = "";
	
	private String viewIntCol = "";
	
	
	public String getViewCol() {
		return viewCol;
	}
	public void setViewCol(String viewCol) {
		this.viewCol = viewCol;
	}
	public String getViewDateCol() {
		return viewDateCol;
	}
	public void setViewDateCol(String viewDateCol) {
		this.viewDateCol = viewDateCol;
	}
	public String getViewDoubleCol() {
		return viewDoubleCol;
	}
	public void setViewDoubleCol(String viewDoubleCol) {
		this.viewDoubleCol = viewDoubleCol;
	}
	public String getViewIntCol() {
		return viewIntCol;
	}
	public void setViewIntCol(String viewIntCol) {
		this.viewIntCol = viewIntCol;
	}
	public ModelPageView(){
		StringBuilder sb = new StringBuilder();
		sb
				.append("					<td><html:text name=\"#{firstEntityL}.#{fieldName}\"> ");
		sb.append(" </html:text></td>\r\n");
		sb.append("					<td><html:property value=\"");
		sb.append("#{firstEntityL}.#{fieldName}\"/></td>\r\n");
		sb.append("");
		setViewCol(sb.toString());

		sb = new StringBuilder();
		sb
				.append("					<td><html:text name=\"#{firstEntityL}.#{fieldName}\"> ");
		sb.append(" </html:text></td>\r\n");
		sb.append("					<td><html:text name=\"global.format.date\">");
		sb.append("<html:param value=\"");
		sb.append("#{firstEntityL}.#{fieldName}\"/></html:text></td>\r\n");
		sb.append("");
		setViewDateCol(sb.toString());

		sb = new StringBuilder();
		sb
				.append("					<td><html:text name=\"#{firstEntityL}.#{fieldName}\"> ");
		sb.append(" </html:text></td>\r\n");
		sb.append("					<td><html:text name=\"global.format.money\">");
		sb.append("<html:param value=\"");
		sb.append("#{firstEntityL}.#{fieldName}\"/></html:text></td>\r\n");
		sb.append("");
		setViewDoubleCol(sb.toString());

		sb = new StringBuilder();
		sb
				.append("					<td><html:text name=\"#{firstEntityL}.#{fieldName}\"> ");
		sb.append(" </html:text></td>\r\n");
		sb.append("					<td><html:text name=\"global.format.int\">");
		sb.append("<html:param value=\"");
		sb.append("#{firstEntityL}.#{fieldName}\"/></html:text></td>\r\n");
		sb.append("");
		setViewIntCol(sb.toString());
	}
	@Override
	public void product() {
		// TODO Auto-generated method stub
		try {
			File file = new File(getPagePath("view.jsp"));
			String name = "page_view.txt";
			StringBuilder sb = new StringBuilder();
			File fileXML = new File(model.getFilePath(packName, name));
			String mainTxt = model.readTpl(fileXML);
			String dataBody = productViewDataBody();
			mainTxt = mainTxt.replaceAll("\\#\\{dataBody\\}", dataBody);
			sb.append(mainTxt);
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());
			if (file != null) {
				file = null;
			}
			if (fileXML != null) {
				fileXML = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private String productViewDataBody() {
		StringBuilder sb = new StringBuilder();
		int i = 0;
		sb.append("<tr>\r\n");
		for (Field field : fields) {
			if (field == null) {
				break;
			}
			if (i % 2 == 0 && i != 0) {
				sb.append("</tr>\r\n<tr>\r\n");
			}
			String typeName = field.getType().getName();
			typeName = typeName.toLowerCase();
			String col = "";
			if (typeName.indexOf("date") > 0) {
				col = viewDateCol.replaceAll("\\#\\{firstEntityL\\}",
						model.getFirstEntityL());
			} else if (typeName.indexOf("int") > 0) {
				col = viewIntCol.replaceAll("\\#\\{firstEntityL\\}",
						model.getFirstEntityL());
			} else if (typeName.indexOf("double") > 0) {
				col = viewDoubleCol.replaceAll("\\#\\{firstEntityL\\}",
						model.getFirstEntityL());
			} else {
				col = viewCol.replaceAll("\\#\\{firstEntityL\\}",
						model.getFirstEntityL());
			}
			col = col.replaceAll("\\#\\{fieldName\\}", field.getName());
			sb.append(col);

			i++;
		}
		if (!(i % 2 == 0)) {
			sb.append("	<td colspan=\"2\">&nbsp;</td>\r\n");
			sb.append("</tr>\r\n");
		}
		return sb.toString();
	}
}
